//
//  NSDate+Extension.swift
//  SinaWeibo_18
//
//  Created by apple on 15/10/23.
//  Copyright © 2015年 cz. All rights reserved.
//

import Foundation

extension NSDate{
    //是今年
     func isThisYear(target: NSDate) -> Bool{
        let formatter =  NSDateFormatter()
        //获得当前时间
//        let currentDate = NSDate()
        formatter.dateFormat = "yyyy"
        
        //取到我们要对比的date的年份字符串
        let targetStr = formatter.stringFromDate(target)
        //取到当前时间的年份字符串
//        let currentStr = formatter.stringFromDate(currentDate)
        
        let currentStr = formatter.stringFromDate(self)
        return (targetStr as NSString).isEqualToString(currentStr)
        
    }
    //是今天
     func isToday(target: NSDate) -> Bool{
        
//        let currentDate = NSDate()
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        let targetStr = formatter.stringFromDate(target)
        let currentStr = formatter.stringFromDate(self)
        
        return (targetStr as NSString).isEqualToString(currentStr)
    }
    //是昨天
     func isYesterday(var target: NSDate) -> Bool{
        var currentDate = NSDate()
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        let targetStr = formatter.stringFromDate(target)
        let currentStr = formatter.stringFromDate(currentDate)
        //2015-10-10
        target = formatter.dateFromString(targetStr)!
        //2015-9-9
        currentDate = formatter.dateFromString(currentStr)!
        //获得俩时间间隔
        let result = currentDate.timeIntervalSinceDate(target)
        if result == 24*60*60{
            return true
        }
        
        return false
    }
    //是昨天-2
     func isYesterday2(var target: NSDate) -> Bool{
        var currentDate = NSDate()
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        let targetStr = formatter.stringFromDate(target)
        let currentStr = formatter.stringFromDate(currentDate)
        //2015-10-10
        target = formatter.dateFromString(targetStr)!
        //2015-9-9
        currentDate = formatter.dateFromString(currentStr)!
        
        //获取当前日历对象
        let calendar = NSCalendar.currentCalendar()
        let components = calendar.components([NSCalendarUnit.NSDayCalendarUnit], fromDate: target, toDate: currentDate, options: NSCalendarOptions.WrapComponents)
        if components.day == 1 {
            return true
        }
        return false
    }

}